package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.androiduidemo.R;
import com.example.myapplication.javabean.User;

import java.util.Random;

public class addFriend extends AppCompatActivity {
    private MySQLiteOpenHelper1 mySQLiteOpenHelper;
    private EditText mname,mclassName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Compiler EdgeToEdge = null;
        EdgeToEdge.enable();
        setContentView(R.layout.activity_add_friend);
        mySQLiteOpenHelper = new MySQLiteOpenHelper1(this);
        find();

    }
    public void find()
    {

        mname=findViewById(R.id.student_id_add_friend);
        mclassName=findViewById(R.id.student_class_add_friend);

    }
    public void addStudent_(View view) {
        // 获取输入框的值

        String s3 = mname.getText().toString();
        String s4 = mclassName.getText().toString();
        Random random = new Random();
        int i = random.nextInt();
        String a= String.valueOf(i);

        // 创建User对象
        User newUser = new User( a,null,s3, s4); // 确保User类有此构造方法

        // 构建并显示确认添加的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认添加")
                .setMessage("您确定要添加这位学生吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    // 用户点击确认，执行添加操作
                    long registerResult = mySQLiteOpenHelper.register(newUser);
                    if (registerResult != -1) {
                        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "添加失败，已拥有该好友", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", (dialog, which) -> {
                    // 用户点击取消，对话框关闭，不执行添加操作
                    dialog.cancel();
                })
                .create()
                .show();
    }
}